package election.platform;

import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import election.api.OpMap;



public class OpSpace implements Runnable
{
    Map<String,Operation>     ops = new HashMap<String,Operation>();
    List<Operation>     nullOps = new LinkedList<Operation>();
    ServerSocket listener;
    
    public OpSpace(int port) throws Exception
    {
        listener = new ServerSocket(port);
    }

    public void run()
    {
        while (true) try {
            Socket conn = listener.accept();
            ObjectInputStream in = new ObjectInputStream(conn.getInputStream());
            Operation operation = (Operation)in.readObject();
            
            if (operation.input == null || operation.input.length()==0)
                nullOps.add(operation);
            else
                ops.put(operation.input, operation);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public Operation take(String dataset)
    {
        return ops.get(dataset);
    }
}
